//
//  TZYHomeDetailViewController.m
//  Moment
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYHomeDetailViewController.h"

@interface TZYHomeDetailViewController ()<UIWebViewDelegate>

@property(nonatomic,retain)UIWebView *webView;

@end

@implementation TZYHomeDetailViewController


- (void)dealloc
{
    self.webView.delegate = nil;
}

-(void)viewWillAppear:(BOOL)animated{
    [self requestData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.webView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleStr;
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  SCREEN_HEIGHT)];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark --请求数据

-(void)requestData{
    
    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSString *body = [NSString stringWithFormat: @"auth=&client=1&contentid=%@&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6",self.detaId];
    
    
    
    [GetData postUrl:urlStr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSString *htmlStr = [dic objectForKey:@"html"];
        
        
        [self.webView loadHTMLString:htmlStr baseURL:nil];

        
        
        
    } failure:^(NSError *error) {
        
        
    }];

    
    
    
}


#pragma mark --  webView自适应
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    
    NSString *str1 = [NSString stringWithFormat:@"%0.2f",SCREEN_WIDTH-20];
    //webview 自适应高度
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:
                                                     @"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"function ResizeImages() { "
                                                     "var myimg,oldwidth;"
                                                     "var maxwidth= %@;"//缩放系数,调动这个参数可以改变图片的宽度
                                                     "for(i=0;i <document.images.length;i++){"
                                                     "myimg = document.images[i];"
                                                     "oldwidth = myimg.width;"
                                                     "if (oldwidth >= 300) {"
                                                     "myimg.width = maxwidth;"
                                                     "myimg.height = myimg.height;"
                                                     "}"
                                                     "}"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);", str1]];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
    
//    [self hideWaiting];
    
}




#pragma mark -- 消除滚动轮指示器
-(void)hideWaiting
{
    [[self.view viewWithTag:9998] removeFromSuperview];
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
